<template>
  <div class="my-box">
    <!-- 未登录 -->
    <div class="header not-login" v-if="$store.state.usertoken == null">
      <div class="btn-box" @click="Login">
        <i class="iconfont icon-shouji"></i>
        <span class="text">登录</span>
      </div>
    </div>
    <!-- 未登录 -->

    <!-- 已登录 -->
    <div class="header user-info" v-else>
      <div class="base-info">
        <div class="left">
          <!-- 头像 -->
          <van-image
            class="userimg"
            round
            width="1.5rem"
            height="1.5rem"
            :src="user.photo"
            fit="cover"
          />
          <!-- 昵称 -->
          <span class="text">{{ user.name }}</span>
        </div>
        <div class="rigth">
          <van-button
            plain
            type="info"
            size="mini"
            round
            :to="{
              path: '/setuser',
              query: {
                user: JSON.stringify(user),
              },
            }"
            >编辑资料</van-button
          >
        </div>
      </div>
      <!-- 用户数据列表 -->
      <div class="data-status">
        <ul class="li-box">
          <li class="data-item">
            <span class="count">{{ user.art_count }}</span>
            <span class="text">头条</span>
          </li>
          <li class="data-item">
            <span class="count">{{ user.follow_count }}</span>
            <span class="text">关注</span>
          </li>
          <li class="data-item">
            <span class="count">{{ user.fans_count }}</span>
            <span class="text">粉丝</span>
          </li>
          <li class="data-item">
            <span class="count">{{ user.like_count }}</span>
            <span class="text">获赞</span>
          </li>
        </ul>
      </div>
    </div>
    <!-- 已登录 -->

    <!-- 导航 -->
    <van-grid clickable :column-num="2" class="user-nav">
      <van-grid-item>
        <template v-slot:icon>
          <i class="iconfont icon-shoucang"></i>
        </template>
        <template v-slot:text>
          <span class="text">收藏</span>
        </template>
      </van-grid-item>
      <van-grid-item>
        <template v-slot:icon>
          <i class="iconfont icon-lishi shouchan"></i>
        </template>
        <template v-slot:text>
          <span class="text">历史</span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- 导航 -->

    <!-- ActionSheet 动作面板 -->
    <van-cell title="消息通知" is-link></van-cell>
    <van-cell title="小智同学" is-link></van-cell>
    <van-cell
      title="退出登录"
      class="not-login"
      clickable
      v-on:click="outLogin"
      v-if="$store.state.usertoken != null"
    ></van-cell>
    <!-- ActionSheet 动作面板 -->
  </div>
</template>

<script>
import { getuserinfo } from "../../api/user";

export default {
  data() {
    return {
      show: false,
      user: {},
    };
  },
  components: {},
  created() {
    this.GetUserInfo();
  },
  methods: {
    Login() {
      this.$router.push("/login");
    },
    outLogin() {
      // 1.弹出提示框
      this.$dialog
        .confirm({
          title: "确认退出",
          message: "确定退出吗，亲",
        })
        .then(() => {
          // 确认
          // 2.清除vuex中的user数据和本地存储的user数据
          //  this.user=null
          this.$store.commit("deleuser");
          window.localStorage.removeItem("TOUTIAO_USER");
        })
        .catch(() => {
          //取消
          return;
        });
    },
    // 获取用户信息
    async GetUserInfo() {
      try {
        const { data: res } = await getuserinfo();
        this.user = res.data;
        console.log(this.user);
      } catch (err) {
        console.log(err, "获取用户信息失败");
      }
    },
  },
};
</script>

<style lang='less' scoped>
// 未登录样式
.my-box {
  .header {
    height: 361px;
    //  background-color: aqua;
    background: url("~@/assets/背景图.jpg");
    background-repeat: no-repeat;
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .btn-box {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }
  .btn-box {
    height: 200px;
    width: 200px;
    background-color: #fff;
    border-radius: 50%;
    .iconfont {
      font-size: 100px;
      color: deepskyblue;
    }
    .text {
      font-size: 30px;
      color: deepskyblue;
    }
  }
}
// 未登录样式

// 已登录样式
.user-info {
  .base-info {
    height: 231px;
    padding: 76px 32px 23px;
    box-sizing: border-box;
    // background-color: palegoldenrod;
    display: flex;
    justify-content: space-between;
    .left {
      display: flex;
      align-items: center;
      .userimg {
        border: 4px solid #fff;
      }
      .text {
        padding-left: 15px;
        color: #fff;
      }
    }
    .rigth {
      display: flex;
      align-items: center;
    }
  }
  .data-status {
    height: 130px;
    // background-color: paleturquoise;
    .li-box {
      display: flex;

      .data-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .count {
          font-size: 36px;
          color: #fff;
          padding-bottom: 5px;
        }
        .text {
          font-size: 23px;
          color: #fff;
        }
      }
    }
  }
}
// 已登录样式
.user-nav {
  .iconfont {
    font-size: 50px;
    color: #eb5253;
  }
  .text {
    font-size: 32px;
  }
  .shouchan {
    color: #ff9d1d;
  }
}
.not-login {
  margin-top: 10px;
  text-align: center;
  color: #eb5253;
}
</style>